package com.zretc.emptraining.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.emptraining.api.MessageAPI;
import com.zretc.emptraining.dao.EmpDao;
import com.zretc.emptraining.dao.UsersDao;
import com.zretc.emptraining.daoimpl.EmpDaoImpl;
import com.zretc.emptraining.daoimpl.UsersDaoImpl;
import com.zretc.emptraining.entity.Dept;
import com.zretc.emptraining.entity.Teach;

/**
 * Servlet implementation class DuanxinServlet
 */
@WebServlet("/CodeServlet")
public class CodeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	UsersDao usersDao = new UsersDaoImpl();
	EmpDao empDao = new EmpDaoImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		// 生成一个6位0~9之间的随机字符串
		StringBuffer buffer = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < 6; i++) {
			buffer.append(random.nextInt(10));
		}
		String mobile = request.getParameter("mobile");
		// 将电话号码存在session
		request.getSession().setAttribute("username", mobile);
		// 通过手机号获得员工编号
		int empNo = usersDao.getUsersEmpNo(mobile).getEmpNo();
		// 通过员工编号获得员工职位
		if (empNo != 0) {
			String job = empDao.getEmpsByNo(empNo).getEmpJob();

			if ("主管".equals(job)) {
				// 获取部门号
				List<Dept> list = usersDao.getDeptNo(empNo);
				int deptNo = list.get(0).getDeptNo();
				System.out.println(deptNo);
				request.getSession().setAttribute("deptNo", deptNo);
				request.getSession().setAttribute("empNo", empNo);
				System.out.println("部门号" + deptNo);
			} else if ("教员".equals(job)) {
				// 将教员号也存储
				List<Teach> list = usersDao.getTeachNo(mobile);
				int teachNo = list.get(0).getTeacherNo();
				request.getSession().setAttribute("teachNo", teachNo);
				request.getSession().setAttribute("empNo", empNo);
				System.out.println("教员号" + teachNo);
			} else {
				// 将员工号也存储
				request.getSession().setAttribute("empNo", empNo);
				System.out.println("员工号" + empNo);
			}
		}
		

		System.out.println("获取到的电话号码:" + mobile);
		System.out.println("生成的随机数:" + buffer);
		// 发送短信
		// boolean flag = MessageAPI.mobileQuery(mobile,buffer.toString());
		boolean flag = true;
		// 返回发送响应
		String msg = "0";
		if (!flag) {
			System.out.println("验证码发送失败");
			Gson(msg, response);
		} else {
			msg = "200";
			request.getSession().setAttribute("code", buffer.toString());
			request.getSession().setAttribute("mobile", mobile);
			System.out.println("验证码发送成功");
			Gson(msg, response);
		}
	}

	public void Gson(String keyword, HttpServletResponse response) throws IOException {
		Gson gson = new Gson();
		String toJson = gson.toJson(keyword);
		PrintWriter printWriter = response.getWriter();
		printWriter.print(toJson);
		printWriter.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
